package com.ruiya.staging.goods.dataio.util;

import com.ruiya.staging.goods.data.grpc.*;
import io.grpc.stub.StreamObserver;

public class GrpcResponseUtils {
    public static void doEchoError(StreamObserver<EchoResponse> responseObserver, int code, String errMsg){
        RpcStatus rpcStatus = RpcStatus.newBuilder().setCode(code).setMemo(errMsg).build();
        EchoResponse response = EchoResponse.newBuilder()
                .setStatus(rpcStatus)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    public static void doCreateGoodsInfoError(StreamObserver<CreateGoodsInfoResponse> responseObserver, int code, String errMsg){
        RpcStatus rpcStatus = RpcStatus.newBuilder().setCode(code).setMemo(errMsg).build();
        CreateGoodsInfoResponse response = CreateGoodsInfoResponse.newBuilder()
                .setStatus(rpcStatus)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    public static void doGetGoodsInfoResponseError(StreamObserver<GetGoodsInfoResponse> responseObserver, int code, String errMsg) {
        RpcStatus rpcStatus = RpcStatus.newBuilder().setCode(code).setMemo(errMsg == null ? "" : errMsg).build();
        GetGoodsInfoResponse response = GetGoodsInfoResponse.newBuilder()
                .setStatus(rpcStatus)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    public static void doLocateGoodsIdError(StreamObserver<LocateGoodsIdResponse> responseObserver, int code, String errMsg) {
        RpcStatus rpcStatus = RpcStatus.newBuilder().setCode(code).setMemo(errMsg == null ? "" : errMsg).build();
        LocateGoodsIdResponse response = LocateGoodsIdResponse.newBuilder()
                .setStatus(rpcStatus)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

}
